/**
 * 后台管理界面的dao层
 */
import { Bmobs } from "../plugins/bmob";
import { Service } from "typedi";
import {NewCourseModel, updateCourseVideo,UpdateCourse,againUpdateCourseVideo } from "../model/NewCourseModel";
import { TableNameInMiddleware } from "../middleware/TableNameInMiddleware";

@Service()
export class AdminDao extends Bmobs{
	
	constructor() {
		super()
	}
	
	public async addNewCourseDao (info: NewCourseModel): Promise<Object> {
		this.setTableName("coures");
		return await this.AddArray(info)
	}
	
	public async updateCourseVideoDao (info: [ updateCourseVideo ]): Promise<Object> {
		this.setTableName("couresVideo");
		return new Promise((resolve, reject) => {
			info.forEach(async value => {
				resolve( await this.AddArray(value))
			});
		});
		
		
	}
	
	public async getAllCourseDao(page: number): Promise<Object>  {

		this.setTableName("coures");
		this.query.skip(page);
		return await this.QueryAll();
	}
	
	public async getCourseInfoDao(id: string): Promise<Object> {
		this.setTableName("coures");
		return await this.QueryOne(id);
	}
	
	public async UpdateCourseDao(Course: UpdateCourse): Promise<Object> {
		this.setTableName("coures");
		
		for (let item in Course) {
			this.query.set(item, Course[item])
		}
		
		return new Promise((resolve, reject) => {
			this.query.save().then(res => {
				resolve(res)
			}).catch(err => {
				this.log.logSymbolsError(err.message);
			})
		});
		
	}
	
	public async getVideoListDao(id: string): Promise<Array<any>> {
		this.setTableName("couresVideo");
		this.query.equalTo("courseObjectid", "==", id);
		return new Promise((resolve, reject) => {
			this.query.find().then(res => {
				resolve(res)
			}).catch(err => {
				this.log.logSymbolsError(err.message)
			});
		});
	}
	
	public async againUpdateCourseVideoDao(Course: [againUpdateCourseVideo]):Promise<Object>  {
		this.setTableName("couresVideo");
		return new Promise((resolve, reject) => {
			Course.forEach(async value => {
				// 有objectId需要更新，反之 没有objectId，需要新增
				if (value.hasOwnProperty("id")) {
					this.query.equalTo("objectId", "==", value['id']);
					resolve(await this.AddArray(value));
				} else {
					resolve(await this.AddArray(value))
				}
			});
		});
		
	}
	
	public async deleteCourseAndVideoDao(id: string):Promise<Object> {
		this.setTableName("coures");
		return new Promise((resolve, reject) => {
			this.query.destroy(id).then(res => {
				this.deleteVideo(id);
				resolve(res);
			}).catch(err => {
				console.log(err)
			});
		});
		
	}
	
	public deleteVideo (id: string) {
		this.setTableName("couresVideo");
		this.query.equalTo("courseObjectid", "==", id);
		this.query.find().then(todos => {
			todos.destroyAll().then(res => {
				// 成功批量删除
				console.log("成功批量删除： ", res)
			}).catch(err => {
				console.log(err)
			});
		});
	}
	
	public async getAllClassDao(): Promise<Object> {
		this.setTableName("couresClass");
		return await this.QueryAll()
	}
	
	public async deleteClassByIdDao(id: string): Promise<Object> {
		this.setTableName("couresClass");
		return new Promise((resolve, reject) => {
			this.query.destroy(id).then(res => {
				resolve(res);
			}).catch(err => {
				this.log.logSymbolsError(err.message)
			});
		});
	}
	
	public async addNewClassDao(title: string): Promise<Object> {
		this.setTableName("couresClass");
		this.query.set("classTitle",title);
		return new Promise((resolve, reject) => {
			this.query.save().then(res => {
				resolve(res)
			}).catch(err => {
				this.log.logSymbolsError(err.message)
			})
		});
	}
	
	/**
	 * 管理员登录
	 * @param params
	 */
	public async adminLoginDao(params: Object): Promise<Object> {
		return new Promise((resolve, reject) => {
			this.BmobObject.User.login(params['username'],params['userpassword']).then(res => {
				resolve(res)
			}).catch(err => {
				resolve(err)
			});
		})
	}
}